import axios from 'axios'
import { showToast } from 'vant'

const request = axios.create({
  baseURL: 'https://geek.itheima.net',
})

request.interceptors.request.use(
  (config) => {
    try {
      const user = jk.getPreference()
      console.log('用户信息:' + JSON.stringify(user))

      if (user) {
        config.headers.Authorization = `Bearer ${user}`
      }
      console.log('获取token:' + config.headers.Authorization)
    } catch (e) {
      showToast({ message: '极客SDK异常' })
    }
    return config
  },
  (err) => {
    Promise.reject(err)
  },
)

export { request }
